package controller

import (
	"strconv"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"

	"goldtree/internal/service"
	"goldtree/utility/response"
)

// StockController 股票控制器
type StockController struct{}

var StockCtrl = StockController{}

// Create 创建股票
func (c *StockController) Create(r *ghttp.Request) {
	// 添加调试日志
	g.Log().Info(r.Context(), "接收到创建股票请求", "method", r.Method, "url", r.URL.String())
	g.Log().Info(r.Context(), "请求头信息", "headers", r.Header)
	g.Log().Info(r.Context(), "请求体", "body", r.GetBodyString())

	var req *service.CreateStockReq
	if err := r.Parse(&req); err != nil {
		g.Log().Error(r.Context(), "解析请求参数失败", "error", err)
		response.JsonExit(r, 1, err.Error())
	}

	g.Log().Info(r.Context(), "解析后的请求数据", "req", req)

	stock, err := service.Stock.Create(r.Context(), req)
	if err != nil {
		g.Log().Error(r.Context(), "创建股票失败", "error", err)
		response.JsonExit(r, 1, err.Error())
	}

	g.Log().Info(r.Context(), "创建股票成功", "stock", stock)
	response.JsonExit(r, 0, "创建成功", stock)
}

// Update 更新股票
func (c *StockController) Update(r *ghttp.Request) {
	var req *service.UpdateStockReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	err := service.Stock.Update(r.Context(), req)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "更新成功")
}

// Delete 删除股票
func (c *StockController) Delete(r *ghttp.Request) {
	idStr := r.Get("id").String()
	id, err := strconv.Atoi(idStr)
	if err != nil || id == 0 {
		response.JsonExit(r, 1, "ID不能为空或格式错误")
	}

	err = service.Stock.Delete(r.Context(), id)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "删除成功")
}

// GetById 根据ID获取股票
func (c *StockController) GetById(r *ghttp.Request) {
	idStr := r.Get("id").String()
	id, err := strconv.Atoi(idStr)
	if err != nil || id == 0 {
		response.JsonExit(r, 1, "ID不能为空或格式错误")
	}

	stock, err := service.Stock.GetById(r.Context(), id)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if stock == nil {
		response.JsonExit(r, 1, "股票不存在")
	}

	response.JsonExit(r, 0, "获取成功", stock)
}

// GetByCode 根据代码获取股票
func (c *StockController) GetByCode(r *ghttp.Request) {
	code := r.Get("code").String()
	if code == "" {
		response.JsonExit(r, 1, "股票代码不能为空")
	}

	stock, err := service.Stock.GetByCode(r.Context(), code)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if stock == nil {
		response.JsonExit(r, 1, "股票不存在")
	}

	response.JsonExit(r, 0, "获取成功", stock)
}

// GetList 获取股票列表
func (c *StockController) GetList(r *ghttp.Request) {
	var req *service.GetStockListReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, err := service.Stock.GetList(r.Context(), req)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "获取成功", result)
}

// BatchCreate 批量创建股票
func (c *StockController) BatchCreate(r *ghttp.Request) {
	var req []*service.CreateStockReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	err := service.Stock.BatchCreate(r.Context(), req)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "批量创建成功")
}

// GetIndustryList 获取行业列表
func (c *StockController) GetIndustryList(r *ghttp.Request) {
	industries, err := service.Stock.GetIndustryList(r.Context())
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "获取成功", industries)
}

// GetMarketList 获取市场列表
func (c *StockController) GetMarketList(r *ghttp.Request) {
	markets, err := service.Stock.GetMarketList(r.Context())
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "获取成功", markets)
}

// Import 导入股票数据
func (c *StockController) Import(r *ghttp.Request) {
	var req *service.StockImportReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, err := service.Stock.ImportStocks(r.Context(), req)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "导入完成", result)
}

// GetStatistics 获取股票统计信息
func (c *StockController) GetStatistics(r *ghttp.Request) {
	result, err := service.Stock.GetStatistics(r.Context())
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "获取成功", result)
}

// Validate 验证股票数据
func (c *StockController) Validate(r *ghttp.Request) {
	var req *service.CreateStockReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, err := service.Stock.ValidateStock(r.Context(), req)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "验证完成", result)
}

// GetSectorList 获取板块列表
func (c *StockController) GetSectorList(r *ghttp.Request) {
	sectors, err := service.Stock.GetSectorList(r.Context())
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "获取成功", sectors)
}

// SyncMarketData 同步市场数据
func (c *StockController) SyncMarketData(r *ghttp.Request) {
	err := service.Stock.SyncMarketData(r.Context())
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "同步成功")
}

// BulkUpdateStatus 批量更新股票状态
func (c *StockController) BulkUpdateStatus(r *ghttp.Request) {
	type BulkUpdateReq struct {
		Codes    []string `json:"codes" v:"required#股票代码列表不能为空"`
		IsActive bool     `json:"is_active"`
	}

	var req BulkUpdateReq
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	err := service.Stock.BulkUpdateStatus(r.Context(), req.Codes, req.IsActive)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "批量更新成功")
}
